package com.xu.system.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xu.system.mapper.SysRoleMenuMapper;
import com.xu.system.model.entity.SysRoleMenu;
import com.xu.system.service.db.ISysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author 许乾伟
 * @since 2024-12-03 03:53:30
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    @Override
    public Boolean saveRoleMenu(Long roleId, List<Long> menuIds) {
        remove(new QueryWrapper<SysRoleMenu>().eq(SysRoleMenu.ROLE_ID, roleId));
        if (CollectionUtils.isEmpty(menuIds)){
            return Boolean.TRUE;
        }
        return saveBatch(menuIds.stream().map(menuId -> new SysRoleMenu().setRoleId(roleId).setMenuId(menuId)).collect(Collectors.toList()));
    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return list(new QueryWrapper<SysRoleMenu>().eq(SysRoleMenu.ROLE_ID, roleId)).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
    }

    @Override
    public List<Long> getMenuIdsByRoleIds(List<Long> roleIds) {
        return list(new QueryWrapper<SysRoleMenu>()
                .in(SysRoleMenu.ROLE_ID,roleIds))
                .stream().map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());
    }
}
